import React, { RefObject, useEffect, useRef, useState } from "react";

import { getFeedList } from "@/api/https";
export default function useIntersection(ref: any) {
  const [list, setList] = useState<any>([]);
  const [bottomFlag, setBottomFlag] = useState<boolean>(true);
  const lockRef = useRef<boolean>(true);
  const size = useRef<number>(5);
  useEffect(() => {
    // getFeedData();
    let intersectionObserver = new IntersectionObserver((entries) => {
        // console.log(entries,"-------");
        
      if (entries[0].isIntersecting && lockRef.current) {
        // console.log("----石佛之");

        lockRef.current = false;
        size.current += 5;
        getFeedData(0, size.current);
      }
    });
    ref?.current && intersectionObserver.observe(ref.current);
  }, []);
  const getFeedData = (startNum: number = 0, pageSize: number = 5) => {
    getFeedList({
      startNum,
      pageSize,
    }).then((resData:any) => {
      setTimeout(() => {
        // console.log(res,"_---");
        
        if (resData?.length > 0) {
          setList(resData);
        }
        if (resData?.length >= 30) {
          // console.log("----执行几次");
          lockRef.current = false;
          setBottomFlag(false);
        } else {
          lockRef.current = true;
        }
      }, 200);
    });
  };
// console.log(list,"----");
  
  //   //bottomFlag  false无新数据
  return [list, bottomFlag];
}
